package com.fosun.sprcore.tools.generatecode.template;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.fosun.sprcore.tools.generatecode.base.BaseTemplate;
import com.fosun.sprcore.tools.generatecode.base.GenerateCodeMain;
import com.fosun.sprcore.tools.generatecode.template.crudmodulefile.AdditionListSelectField;
import com.fosun.sprcore.tools.generatecode.utils.ExtStringUtils;

public class CrudModuleFileTemplate extends BaseTemplate {
	private final static String TEMPLATE_PATH = "sprcore/CrudModuleFileTemplate/";
	@Override
	public void generate(GenerateCodeMain main) throws Exception {
		String moduleNameU = StringUtils.capitalize(moduleName);
		String dbTablePrefixU = StringUtils.capitalize(dbTablePrefix);
		
		String packageDir = (this.getPackageName()+".api").replaceAll("\\.", "/");
		
//		Map<String, String> data = new HashMap<String, String>();
		//data.put("projectWar", projectWar);
//		data.put("moduleName", moduleName);
//		data.put("packageName", packageName);
//		data.put("moduleNameU", moduleNameU);
//		data.put("dbTablePrefix", dbTablePrefix);
//		data.put("dbTablePrefixU", dbTablePrefixU);	
		main.mkdir("/java/"+packageDir+"/"+moduleName + "/bussiness/impl");
		main.mkdir("/java/"+packageDir+"/"+moduleName + "/bussiness/model");
		
		Map<String, Object> data = getData(this);
		
		main.generate(TEMPLATE_PATH + "java/control/Control.java", "java/"+packageDir+"/"+moduleName + "/control/" + moduleNameU + "Control.java", data);
		main.generate(TEMPLATE_PATH + "java/service/Service.java", "java/"+packageDir+"/"+moduleName + "/service/" + moduleNameU + "Service.java", data);
		main.generate(TEMPLATE_PATH + "java/service/impl/ServiceImpl.java", "java/"+packageDir+"/"+moduleName + "/service/impl/" + moduleNameU + "ServiceImpl.java", data);
		
		main.generate(TEMPLATE_PATH + "java/service/model/CreateOrUpdateSpModel.java", "java/"+packageDir+"/"+moduleName + "/service/model/CreateOrUpdateSpModel.java", data);
		main.generate(TEMPLATE_PATH + "java/service/model/CreateOrUpdateSrModel.java", "java/"+packageDir+"/"+moduleName + "/service/model/CreateOrUpdateSrModel.java", data);
		main.generate(TEMPLATE_PATH + "java/service/model/DeleteSpModel.java", "java/"+packageDir+"/"+moduleName + "/service/model/DeleteSpModel.java", data);
		main.generate(TEMPLATE_PATH + "java/service/model/DeleteSrModel.java", "java/"+packageDir+"/"+moduleName + "/service/model/DeleteSrModel.java", data);
		main.generate(TEMPLATE_PATH + "java/service/model/GetSpModel.java", "java/"+packageDir+"/"+moduleName + "/service/model/GetSpModel.java", data);
		main.generate(TEMPLATE_PATH + "java/service/model/GetSrModel.java", "java/"+packageDir+"/"+moduleName + "/service/model/GetSrModel.java", data);
		main.generate(TEMPLATE_PATH + "java/service/model/ListSpModel.java", "java/"+packageDir+"/"+moduleName + "/service/model/ListSpModel.java", data);
		main.generate(TEMPLATE_PATH + "java/service/model/ListSrModel.java", "java/"+packageDir+"/"+moduleName + "/service/model/ListSrModel.java", data);
		
		main.generate(TEMPLATE_PATH + "java/dao/Dao.java", "java/"+packageDir+"/"+moduleName + "/dao/" + moduleNameU + "Dao.java", data);
		main.generate(TEMPLATE_PATH + "java/dao/xml/Mapper.xml", "java/"+packageDir+"/"+moduleName + "/dao/xml/" + moduleNameU + "Mapper.xml", data);
		
		
//		main.generate(TEMPLATE_PATH + "html/iview/index.vue", "html/sprappview/"+moduleName+"/" + "index.vue", data);
//		main.generate(TEMPLATE_PATH + "html/iview/index.less", "html/sprappview/"+moduleName+"/" + "index.less", data);
//		main.generate(TEMPLATE_PATH + "html/iview/index.js", "html/sprappview/"+moduleName+"/" + "index.js", data);
		
		main.generate(TEMPLATE_PATH + "html/iview/index.vue", "html/sprappview/"+moduleName + ".vue", data);
		main.generate(TEMPLATE_PATH + "html/iview/index.less", "html/sprappview/"+moduleName + ".less", data);
		main.generate(TEMPLATE_PATH + "html/iview/index.js", "html/sprappview/"+moduleName + ".js", data);
	}
 
	
	
	
	private String packageName;//eg:com.fimuz.mqmeet	
	private String moduleName;//eg:Boardroom
	private String dbTablePrefix;//表前缀,eg:spr
	private String dbTablePrefixTag;//前缀分隔符 ，eg:-
	private String dbTableName;//hxz_meetroom
	private String dbTableJavaName;//hxzMeetRoom
	private String uniqueColumn;//唯一的字段login_no
	private String uniqueColumnJavaName;//loginNo
	private String[] keywordColumns;//搜索的字段
	private AdditionListSelectField[] additionListSelectFields;//在查询页面，select额外的字段
	private String moduleLabel;//会议室
	private List<TableColumnModel> tableColumnModelList = new ArrayList<TableColumnModel>();//字段清单
	private String insertModelJavaAddtionAtService;//新增额外要执行的命令，用于执行设置默认值；
	private String updateModelJavaAddtionAtService;//修改额外要执行的命令，用于执行设置默认值；
	
	
 
 

 
 
	public String getInsertModelJavaAddtionAtService() {
		return insertModelJavaAddtionAtService;
	}
	/**
	 * 新增额外要执行的命令，用于执行设置默认值；
	 * setInsertModelJavaAddtionAtService("db.setPublishStatus(\"published\");db.setPublishTime(new Date());");
	 * @param insertModelJavaAddtion
	 */
	public void setInsertModelJavaAddtionAtService(String insertModelJavaAddtionAtService) {
		this.insertModelJavaAddtionAtService = insertModelJavaAddtionAtService;
	}
	public String getUpdateModelJavaAddtionAtService() {
		return updateModelJavaAddtionAtService;
	}
	/**
	 * 修改额外要执行的命令，用于执行设置默认值；
	 * setUpdateModelJavaAddtionAtService("db.setPublishStatus(\"published\");db.setPublishTime(new Date());");
	 * @param updateModelJavaAddtion
	 */
	public void setUpdateModelJavaAddtionAtService(String updateModelJavaAddtionAtService) {
		this.updateModelJavaAddtionAtService = updateModelJavaAddtionAtService;
	}

 
	public AdditionListSelectField[] getAdditionListSelectFields() {
		return additionListSelectFields;
	}
	/**
	 * usage:<BR>
	 * <pre>
t.setAdditionListSelectFields(new AdditionListSelectField[] {
	new AdditionListSelectField("(SELECT a.user_name FROM spr_uuc_user a WHERE a.id=t1.user_id) user_name","用户")
});
	 *	</pre>
	 * @param additionListSelectFields
	 */
	public void setAdditionListSelectFields(AdditionListSelectField[] additionListSelectFields) {
		if(additionListSelectFields==null) {
			additionListSelectFields = new AdditionListSelectField[0];
		}
		this.additionListSelectFields = additionListSelectFields;
	}
	public String getUniqueColumnJavaName() {
		return uniqueColumnJavaName;
	}
	public void setUniqueColumnJavaName(String uniqueColumnJavaName) {
		this.uniqueColumnJavaName = uniqueColumnJavaName;
	}
	public String getUniqueColumn() {
		return uniqueColumn;
	}
	public void setUniqueColumn(String uniqueColumn) {
		this.uniqueColumn = uniqueColumn;
		this.uniqueColumnJavaName = ExtStringUtils.underlineToCamel(uniqueColumn);
	}
	public String getDbTablePrefixTag() {
		return dbTablePrefixTag;
	}
	public void setDbTablePrefixTag(String dbTablePrefixTag) {
		this.dbTablePrefixTag = dbTablePrefixTag;
	}
	public String getDbTableName() {
		return dbTableName;
	}
	public void setDbTableName(String dbTableName) {
		this.dbTableName = dbTableName;
		this.dbTableJavaName = ExtStringUtils.underlineToCamel(dbTableName);
	}
	public String getDbTableJavaName() {
		return dbTableJavaName;
	}
	public void setDbTableJavaName(String dbTableJavaName) {
		this.dbTableJavaName = dbTableJavaName;
	}
	public String getPackageName() {
		return packageName;
	}
	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}
	public String getModuleName() {
		return moduleName;
	}
	public void setModuleName(String moduleName) {
		this.moduleName = moduleName;
	}
	public String getDbTablePrefix() {
		return dbTablePrefix;
	}
	public void setDbTablePrefix(String dbTablePrefix) {
		this.dbTablePrefix = dbTablePrefix;
	}
	public List<TableColumnModel> getTableColumnModelList() {
		return tableColumnModelList;
	}
	public void setTableColumnModelList(List<TableColumnModel> tableColumnModelList) {
		this.tableColumnModelList = tableColumnModelList;
	}

	public String getModuleLabel() {
		return moduleLabel;
	}
	public void setModuleLabel(String moduleLabel) {
		this.moduleLabel = moduleLabel;
	}
	

 

	public String[] getKeywordColumns() {
		return keywordColumns;
	}
	public void setKeywordColumns(String[] keywordColumns) {
		this.keywordColumns = keywordColumns;
	}




	public static class TableColumnModel {

		private String columnType;
		private String columnType_display_inListLabelSql;
		
		private String fieldName;
		private String javaFieldName;
		private String javaFieldNameU;
		private String dataType;
		private String comment;
//		private String isPk;//是否是Pk
		private String isNotNull;
		private String displayType;
		private String displayTypeValue;
		private String isInList;

		//private String isInListSearchKey;//是否是查询关键字
		private String isInCreate;
		private String isInUpdate;
		public String getFieldName() {
			return fieldName;
		}
		/**
		 * 字段名,eg:room_name
		 * @param fieldName
		 */
		public void setFieldName(String fieldName) {
			this.fieldName = fieldName;
			this.javaFieldName = ExtStringUtils.underlineToCamel(fieldName);
		}
		public String getDataType() {
			return dataType;
		}
		/**
		 * 字段类型
		 * string,int(todo)
		 */
		public void setDataType(String dataType) {
			this.dataType = dataType;
		}
		public String getComment() {
			return comment;
		}
		/**
		 * 字段名称
		 * @param comment
		 */
		public void setComment(String comment) {
			this.comment = comment;
		}
		public String getIsNotNull() {
			return isNotNull;
		}
		/**
		 * 是否不能为空
		 * 1：不能为空，必填
		 * 0：可以为空
		 * @param isNotNull
		 */
		public void setIsNotNull(String isNotNull) {
			this.isNotNull = isNotNull;
		}
		public String getDisplayType() {
			return displayType;
		}
		/**
		 * <pre>
		 * 显示字段类型
		 * input_text
		 * input_number
		 * select_remote
		 * radio_dict
		 * textarea
		 * </pre>
		 */
		public void setDisplayType(String displayType) {
			this.displayType = displayType;
		}
		public String getDisplayTypeValue() {
			return displayTypeValue;
		}
		/**
		 * 显示字段对应的值
		 * input_text
		 * select_remote:common的调用方法,如/api/common/listBuildingregion
		 * radio_dict:dict名称
		 * textarea
		 */
		public void setDisplayTypeValue(String displayTypeValue) {
			this.displayTypeValue = displayTypeValue;
		}
		public String getIsInList() {
			return isInList;
		}
 
		/**
		 * 是否在list中查询
		 * @param isInList
		 */
		public void setIsInList(String isInList) {
			this.isInList = isInList;
		}
		public String getIsInCreate() {
			return isInCreate;
		}
		/**
		 * 是否是在新增页面
		 * @param isInCreate
		 */
		public void setIsInCreate(String isInCreate) {
			this.isInCreate = isInCreate;
		}
		public String getIsInUpdate() {
			return isInUpdate;
		}
		/**
		 * 是否是在修改页面
		 * @param isInCreate
		 */
		public void setIsInUpdate(String isInUpdate) {
			this.isInUpdate = isInUpdate;
		}
		public String getJavaFieldName() {
			return javaFieldName;
		}
		/**
		 * java字段名，驼峰,eg:roomName
		 * @param javaFieldName
		 */
		public void setJavaFieldName(String javaFieldName) {
			this.javaFieldName = javaFieldName;
		}
		public String getJavaFieldNameU() {
			return StringUtils.capitalize(javaFieldName);
		}
		public void setJavaFieldNameU(String javaFieldNameU) {
			this.javaFieldNameU = javaFieldNameU;
		}
		/*
		 * 是否显示 column:display显示字段，db数据库字段
		 * display通常用于查询列表中id转换为中文显示
		 * 		如果是显示 column(display),则只需要如下字段：
		 * 		fieldName
		 * 		comment
		 * 		setIsInList
		 * 		columnType_display_inListLabelSql：SELECT ab.title FROM v2_article ab WHERE ab.id=t1.catalog_id
		 */
		public String getColumnType() {
			return columnType;
		}
		/**
		 * display
		 * 默认为db
		 * @param columnType
		 */
		public void setColumnType(String columnType) {
			this.columnType = columnType;
			if(columnType.equals("display") ){
				setDataType("string");
				setIsInCreate("0");
				setIsInUpdate("0");
				setIsNotNull("0");
				setDisplayType("input_text");
			}
		}
		public String getColumnType_display_inListLabelSql() {
			return columnType_display_inListLabelSql;
		}
		/**
		 * 如果isInList=1，则该字段在list的值（通常是id，在列表却要显示id对应的中文）
		 * t1表为当前配置的表
		 * @param inListLabelSql
		 */
		public void setColumnType_display_inListLabelSql(String columnType_display_inListLabelSql) {
			this.columnType_display_inListLabelSql = columnType_display_inListLabelSql;
			
		}

 
		
		

	}
	
}
